package com.itheima.thread.communication.demo05;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class CustomerThread implements Runnable {

    private Box box;
    private ReentrantLock reentrantLock;
    private Condition condition;
    public CustomerThread(Box box, ReentrantLock reentrantLock, Condition condition) {
        this.box = box;
        this.reentrantLock = reentrantLock;
        this.condition = condition;
    }

    @Override
    public void run() {

        while (true) {

            reentrantLock.lock();

            if(!box.isFlag()) {
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println(box.getMilkType() + "----" + box.getAmount());

            box.setFlag(false);

            condition.signal();

            reentrantLock.unlock();

        }

    }
}
